package com.ihr360.payroll.controller.attendanceReport;

import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.commons.exception.Ihr360Exception;
import com.ihr360.commons.vo.PageData;
import com.ihr360.commons.vo.ResultInfo;
import com.ihr360.excel.vo.ImportRequest;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.payroll.entity.attendance.AttendanceBillEntity;
import com.ihr360.payroll.model.dto.attendance.report.AttendanceBill;
import com.ihr360.payroll.model.response.AttendanceBillResponse;
import com.ihr360.payroll.request.AttendanceBillRequest;
import com.ihr360.payroll.response.attendance.ReportTypeResponse;
import com.ihr360.payroll.service.attendancereport.AttendanceBillService;
import com.ihr360.rest.service.SimpleRestService;
import com.ihr360.rest.webmvc.AbstractRestSimpleWithLongIdController;
import com.ihr360.shared.basicstaffinfo.dao.SyncedStaffInfoRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by Eric on 2018/3/23/023.
 */
@Api("考勤日报接口")
@Controller
@RequestMapping(value = "/attendanceDailyReport")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.SALARY_CODE,
        mainModuleName = ModuleCodeStore.Module.SALARY_NAME,
        subModuleCode = ModuleCodeStore.Module.Salary.SALARY_ATTENDANCE_CODE,
        subModuleName = ModuleCodeStore.Module.Salary.SALARY_ATTENDANCE_NAME,
        hasExtendsAbstractRestController = true)
public class AttendanceBillController extends AbstractRestSimpleWithLongIdController<AttendanceBill> {

    @Autowired
    private AttendanceBillService attendanceBillService;
    @Autowired
    private SyncedStaffInfoRepository syncedStaffInfoRepository;

    @Override
    public SimpleRestService<AttendanceBill, Long> getSimpleRestService() {
        return attendanceBillService;
    }

    @FunctionDefinition(functionName = "查询当年周期个人所有单据（个人）", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "查询当年周期个人所有单据（个人）")
    @PostMapping(value = "/staff/all/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public AttendanceBillResponse queryStaffAll(AuthUserInfo userInfo, @RequestBody AttendanceBillRequest attendanceBillRequest) {
        AttendanceBillResponse attendanceBillResponse = new AttendanceBillResponse();
        attendanceBillResponse.setPageData(attendanceBillService.getStaffAllAttendanceBill(userInfo.getCompanyId(), attendanceBillRequest));
        attendanceBillResponse.setBasicStaffInfo(syncedStaffInfoRepository.findByCompanyIdAndId(userInfo.getCompanyId(), attendanceBillRequest.getStaffId()));
        return attendanceBillResponse;
    }

    @FunctionDefinition(functionName = "查询当前周期所有单据列表（所有）", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "/attendanceDailyReport/all/list", notes = "查询当前周期所有单据列表（所有）")
    @PostMapping(value = "/all/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public PageData<AttendanceBill> queryAll(AuthUserInfo userInfo, @RequestBody AttendanceBillRequest attendanceBillRequest) {
        return attendanceBillService.getAllAttendanceBill(userInfo.getCompanyId(), attendanceBillRequest);
    }

    @FunctionDefinition(functionName = "考勤单据明细导入", functionCode = ModuleCodeStore.Function.IMPORT)
    @ApiOperation(value = "/attendanceDailyReport/upload", notes = "考勤单据明细导入")
    @PostMapping(value = "/upload")
    public ResultInfo billUpload(AuthUserInfo userInfo, @RequestBody ImportRequest importRequest) {
        ResultInfo resultInfo = new ResultInfo();
        attendanceBillService.importAttendanceBill(userInfo.getCompanyId(), importRequest);
        resultInfo.setCode(0);
        resultInfo.setMessage("考勤汇总导入成功");
        return resultInfo;
    }

    @FunctionDefinition(functionName = "考勤单据明细模板下载", functionCode = ModuleCodeStore.Function.IMPORT)
    @ApiOperation(value = "/attendanceDailyReport/template", notes = "考勤单据明细模板下载")
    @GetMapping(value = "/template", produces = ("text/html;charset=UTF-8"))
    public void billTemplate(HttpServletResponse response, @RequestParam Long summaryId) throws IOException {
        response.setHeader("x-filename", URLEncoder.encode("考勤单据导入模板.xls", "UTF-8"));
        attendanceBillService.excelTemplateDownload(response.getOutputStream(), summaryId);
    }

    @FunctionDefinition(functionName = "考勤单据类型列表查询", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "/attendanceDailyReport/reportType/list", notes = "考勤单据类型列表查询")
    @GetMapping(value = "/reportType/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<ReportTypeResponse> queryReportList(@RequestParam Long summaryId, @RequestParam String type) {
        return attendanceBillService.queryReportList(summaryId, type);
    }

    @FunctionDefinition(functionName = "考勤单据修改", functionCode = ModuleCodeStore.Function.UPDATE)
    @ApiOperation(value = "/attendanceDailyReport/updateBill", notes = "考勤单据类型列表查询")
    @PostMapping(value = "/updateBill", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void updateAttendanceBill(@RequestBody AttendanceBillEntity entity) {
        AttendanceBill attendanceBill = attendanceBillService.getOne(entity.getId());
        if (attendanceBill == null) {
            throw new Ihr360Exception(null, "考勤单据不存在");
        }
        BeanUtils.copyProperties(entity, attendanceBill);
        attendanceBillService.createOrUpdate(entity.getId(), attendanceBill);
    }
}
